package com.ruoyi.business.service.impl;

import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.LoanOffset;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.ILoanOffsetService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.DoLoanRepaymentService;
import com.ruoyi.business.service.IRepayMentService;
import com.ruoyi.common.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author wqg
 * @description
 * @date 2022/5/12
 */
@Service
public class RepaymentServiceImpl implements IRepayMentService {

    @Autowired
    ILoanOffsetService loanOffsetService;
    @Autowired
    DoLoanRepaymentService doLoanRepaymentService;

    @Override
    public void repaymentNoLock(RepaymentOrder repaymentOrder) {
        //执行冲账
        LoanOffset loanOffset = saveLoanOffset(repaymentOrder);
        doLoanRepaymentService.doLoanRepaymentGetInfo(loanOffset);
    }





    @Override
    public void repaymentNoLock(LoanIou loanIou, String repaymentSn, String actionType, BigDecimal amt, BigDecimal reduceFee, int type) {
        LoanOffset loanOffset = saveLoanOffset(loanIou, repaymentSn, actionType, amt, reduceFee, type);
        //冲账
        doLoanRepaymentService.doLoanRepaymentGetInfo(loanOffset);
    }

    private LoanOffset saveLoanOffset(RepaymentOrder repaymentOrder) {
        LoanOffset loanOffset = new LoanOffset();
        loanOffset.setCustNo(repaymentOrder.getCustNo());
        loanOffset.setLoanSerialNo(repaymentOrder.getLoanSerialNo());
        loanOffset.setRepaymentNo(repaymentOrder.getRepaymentNo());
        loanOffset.setActionType(repaymentOrder.getActionType());
        loanOffset.setAmt(repaymentOrder.getRepaymentAmt());

        loanOffset.setLoanPrincipal(repaymentOrder.getLoanPrincipal());
        loanOffset.setRepaidPrincipal(repaymentOrder.getRepaidPrincipal());
        loanOffset.setServiceFee(repaymentOrder.getServiceFee());
        loanOffset.setRepaidServiceFee(repaymentOrder.getRepaidServiceFee());
        loanOffset.setLoanInterest(repaymentOrder.getLoanInterest());
        loanOffset.setRepaidInterest(repaymentOrder.getRepaidInterest());
        if (repaymentOrder.getLateFee() == null) {
            repaymentOrder.setLateFee(BigDecimal.ZERO);
        }
        if (repaymentOrder.getReduceFee() == null) {
            repaymentOrder.setReduceFee(BigDecimal.ZERO);
        }
        loanOffset.setLateFee(repaymentOrder.getLateFee());// 不需要 wqg沟通过 后期遇到延迟冲账的场景去回溯
        loanOffset.setRepaidLateFee(repaymentOrder.getRepaidLateFee());

        loanOffset.setLoanOffsetStatus(BizConstants.LOAN_OFFSET_STATUS_WAIT);
        loanOffset.setState(UserConstants.BUSINESS_NORMAL);
        loanOffsetService.insertLoanOffset(loanOffset);
        return loanOffset;
    }

    /**
     * @param loanIou
     * @param repaymentSn
     * @param actionType
     * @param amt
     * @param reduceFee
     * @param type
     * @return
     */
    private LoanOffset saveLoanOffset(LoanIou loanIou, String repaymentSn, String actionType, BigDecimal amt, BigDecimal reduceFee, int type) {
        LoanOffset loanOffset = new LoanOffset();
        loanOffset.setCustNo(loanIou.getCustNo());
        loanOffset.setLoanSerialNo(loanIou.getLoanSerialNo());
        if (type == 1) {
            loanOffset.setRepaymentNo(repaymentSn);
        } else {
            loanOffset.setRemissionNo(repaymentSn);
        }

        loanOffset.setActionType(actionType);
        loanOffset.setAmt(amt);

        loanOffset.setLoanPrincipal(loanIou.getLoanPrincipal());
        loanOffset.setRepaidPrincipal(loanIou.getRepaidPrincipal());
        loanOffset.setServiceFee(loanIou.getServiceFee());
        loanOffset.setRepaidServiceFee(loanIou.getRepaidServiceFee());
        loanOffset.setLoanInterest(loanIou.getLoanInterest());
        loanOffset.setRepaidInterest(loanIou.getRepaidInterest());
        loanOffset.setLateFee(loanIou.getLateFee().subtract(reduceFee));
        loanOffset.setRepaidLateFee(loanIou.getRepaidLateFee());
        loanOffset.setReduceAmt(reduceFee);

        loanOffset.setLoanOffsetStatus(BizConstants.LOAN_OFFSET_STATUS_WAIT);
        loanOffset.setState(UserConstants.BUSINESS_NORMAL);
        loanOffsetService.insertLoanOffset(loanOffset);
        return loanOffset;
    }
}
